﻿@{
    ViewBag.Title = "发表主题 - 小博客";
    Layout = "~/Views/Shared/_BlogLayout.cshtml";
    var m = ViewContext.ViewData.Model as ArticleModel;
    var cateIds = string.Join(",", m.CategoryModels.Select(x => x.ID));
    var allCates = ViewBag.cates as List<CategoryModel>;
    var tags = m.TagModels.Count > 0 ? string.Join(",", m.TagModels.OrderBy(x => x.ID).Select(x => x.TagName)) : "";
}
@using MyBlog
<link href="~/Content/Blog/Release.css" rel="stylesheet" />
<div style="padding:5px">
    <div id="blogTab" class="tab-container">
        <ul class='etabs'>
            <li class='tab'><a href="#tabArticle">发表随笔</a></li>
            <li class='tab'><a href="#tabArticleList">随笔管理</a></li>
            <li class='tab'><a href="#tabCategory">类别管理</a></li>
        </ul>
        <div class="panel-container">
            <div id="tabArticle">
                <div class="dvfont">随笔标题</div>
                <div>
                    <input class="title inputFilter inputbox" type="text" name="title" id="title" maxlength="30" placeholder="请填写标题" value="@m.Title" />
                </div>
                <div class="dvfont">随笔内容</div>
                <div>
                    <script id="content" name="content" class="ueditorContent" style="width:100%;height:400px;" type="text/plain">
                    </script>
                </div>
                <div class="dvfont">个人分类 <a href="javascript:;" id="linkcate">[编辑分类]</a></div>
                <div id="loadACates" class="dvcate">
                </div>
                <div class="dvfont">Tag标签<a id="tag-tip" href="javascript:;" title="标签是指可以概括文章主要内容的关键词，便于更快更容易定位你的文章。">什么是标签？</a></div>
                <div><input class="inputbox inputFilter" id="TagStr" placeholder="添加标签可以更快地定位你的文章哦…" value="@tags" style="width:57%;" type="text" /><label class="green f13">(多个标签之间用英文逗号“,”分隔，每个标签最多25个字符，最多10个标签)</label></div>
                <div class="dvfont">内容摘要<a id="zhaiyao-link" href="javascript:;" title="摘要是指对文章的简要概括，首页展示时会优先显示摘要，如不填写默认截取文章的前300字。">什么是摘要，为何需要？</a></div>
                <div class="dvtxt"><textarea style="width:98%; height:130px;" id="Profile" maxlength="300" placeholder="建议你输入摘要，这便于别人对你的文章进行快速了解…">@m.Profile</textarea></div>
                <div class="dvfont">发布设置</div>
                <div class="dvchktop">
                    <span><input type="checkbox" id="chktop" @(m.IsTop ? "checked='checked'" : "") />置顶</span>
                    <span><input type="checkbox" id="chkcomment" @(m.ID == 0 ? "checked='checked'" : (m.AllowComment ? "checked='checked'" : "")) />允许评论</span>
                    <span><input type="checkbox" id="chkanony" @(m.ID > 0 ?(!m.AllowAnonymous ? "checked='checked'" : ""):"") />禁止游客访问</span>
                    <span><input type="checkbox" id="chksendIndex" @(m.ID == 0 ? "checked='checked'" : (m.AllowSendIndex ? "checked='checked'" : "")) />推送到小博客首页</span>
                </div>
                <div class="dvbtn">
                    <input class="btnsubmit" type="button" id="btnOK" value="发布" />
                    <input class="btnsubmit" type="button" id="btnCancel" value="取消" />
                </div>
                <div class="dvmsg"><span id="spMsg" class="red"></span></div>
            </div>
            <div id="tabCategory">
                <div>
                    <input class="inputbox2 inputFilter" type="text" name="categoryName" id="categoryName" maxlength="20" />
                    <input class="btn" type="button" id="btnAddCate" value="添加分类" />
                    <span id="spcate" class="red"></span>
                </div>
                <div id="loadcate" style="padding:10px;">
                </div>
            </div>
            <div id="tabArticleList">
                <div class="dvfont">所有分类</div>
                <select class="selcate" onchange="loadArticleByCid()">
                    <option value="0">全部</option>
                    @foreach (var item in allCates.Default())
                    {
                        <option onclick="" value="@item.ID">@(item.CategoryName)(@item.ArticleModels.Count)</option>
                    }
                </select>
                <div id="loadArticleList" style="padding:10px;">
                </div>
            </div>
        </div>
        <input type="hidden" id="hidContent" value="@m.Content" />
        @Html.AntiForgeryToken()
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#blogTab').easytabs();
        $('#zhaiyao-link,#tag-tip').jBox('Tooltip', {
            //content: '这就是摘要！',
            closeOnMouseleave: true,
            theme:'TooltipBorder',
            //position:{x: 'right', y:'top'}
            pointer:'center:-180'
        });
        var ue = UE.getEditor('content');
        ue.ready(function () {
            if (@m.ID>0) {
                ue.setContent($("#hidContent").val());
                @*ue.setContent('@Html.Raw(m.Content.HtmlDecode())');*@
            }
        });
      
        $("#title").focus();
        $("#btnOK").click(function(){
            if (!$.trim($("#title").val())) {
                $("#spMsg").html("随笔标题还没写哦！")
                $("#title").focus();
                return;
            }
            $("#spMsg").html("");
            if (!ue.hasContents()) {
                $("#spMsg").html("随笔内容还没写哦！")
                ue.focus();
                return;
            }
            if (ue.getContentTxt().length < 10) {
                $("#spMsg").html("随笔内容不能少于10个字符！")
                ue.focus();
                return;
            }
            if ($.trim($('#Profile').val())&&$('#Profile').val().length>300) {
                $("#spMsg").html("摘要不能超过300个字哦！")
                $('#Profile').focus();
                return;
            }
            $("#spMsg").html("");
            $("#btnOK").attr("disabled","disabled");
            $.post("@Url.Action("Release")",
                {
                    id:@m.ID,
                    title:$.htmlEncode($.trim($("#title").val())),
                    content:$.htmlEncode(ue.getContent()),
                    cids:getCateIds(),
                    TagStr:$('#TagStr').val(),
                    Profile:$.htmlEncode($('#Profile').val()),
                    isTop:$("#chktop").is(":checked"),
                    AllowComment:$("#chkcomment").is(":checked"),
                    AllowAnonymous:!$("#chkanony").is(":checked"),
                    AllowSendIndex:$("#chksendIndex").is(":checked"),
                    __RequestVerificationToken:$("input[name='__RequestVerificationToken']").val()
                },
                function(obj){
                    if (obj.State!=1) {
                        $.showAjaxMessage(obj, $("#spMsg"), null);
                    }else{
                        layerMsg('发布成功！',function(){location.href="@Url.Action("Article/")" + obj.Data;});
                    }
                },"json");

        });
        $("#btnCancel").click(function(){
            location.href=location.href.substr(0,location.href.lastIndexOf("/"));
        });

        $("#linkcate").click(function(){
            showCateTab();
        });
        $("#btnAddCate").click(function(){
            if (!$.trim($("#categoryName").val())) {
                $("#spcate").show().html("请填写类别！");
                $("#categoryName").focus();
                return;
            }
            $.post("@Url.Action("AddCategory")",{categoryName:$.trim($("#categoryName").val())},
                    function(obj){
                        if (obj.State==1) {
                            $("#spcate").css("width",200).html("添加成功！").show().hide(2000);
                            $("#categoryName").val('');
                            loadAllCates();
                            loadArticleCates();
                        }else{
                            $.showAjaxMessage(obj, $("#spcate").show(), null);
                        }
                    });
        });
        loadArticleCates();
        loadAllCates();
        loadArticleList(0);
    });

    function loadArticleCates(){
        $("#loadACates").load("@Url.Action("_LoadArticleCategory", new {  ids = cateIds })");
    }

    function loadAllCates(){
        $("#loadcate").load("@Url.Action("_LoadCategory")");
    }

    function loadArticleList(cid){
        $("#loadArticleList").load("@Url.Action("_ArticleAjaxPageList")",{cid:cid});
    }

    function showCateTab(){
        $('#blogTab').easytabs('select', '#tabCategory');
        $("#categoryName").focus();
    }
    function getCateIds(){
        var ids=[];
        $("#loadACates :checked").each(function(i,c){
            ids.push(c.value);
        });
        return ids.toString();//.join();效果一样的，默认逗号分隔
    }

    function saveCate(o, id) {
        if (!$.trim($("#ipcate"+id).val())) {
            $("#spcate" + id).show().html("请输入类别名称");
            $("#ipcate"+id).focus();
        } else {
            $("#spcate" + id).html("");
            $.post("@Url.Action("EditCategory")", { ID: $("#hid" + id).val(), newName: $.trim($("#ipcate"+id).val()) }, function (json) {
                if (json.State == 1) {
                    $("#lblcate" + id).html($.trim($("#ipcate"+id).val()));
                    $("#lblcate" + id).show();
                    $("#ipcate" + id).hide();
                    $("#savecate" + id).hide();
                    $("#cancelcate" + id).hide();
                    $("#spcate" + id).css("width",200).html("保存成功！").show().hide(2000);
                    loadArticleCates();
                } else {
                    if (json.Message != undefined && json.Message != "") {
                        $("#spcate" + id).show().html(json.Message);
                    }
                }
            }, "json");
        }
    }

    function cancelCate(id) {
        $("#lblcate" + id).show();
        $("#ipcate" + id).hide();
        $("#savecate" + id).hide();
        $("#cancelcate" + id).hide();
        $("#spcate" + id).html("").hide();
    }
    function editCate(id) {
        $("#lblcate" + id).hide();
        $("#ipcate" + id).show();
        $("#savecate" + id).show();
        $("#cancelcate" + id).show();
        $("#ipcate" + id).focus();
    }

    function deleteCate(o, id) {
        var showtip=$("#lblACount" + id).html();
        var msg=showtip!= "0"?"该类别下存在关联的文章，删除这个类别将移除关联，确定删除吗？":"确定删除这个类别吗？";
        layerConfirm(msg,function(){
            $.post("@Url.Action("DeleteCategory")", { id: $("#hid" + id).val() }, function (json) {
                if (json.State == 1) {
                    layerMsg('删除成功！');
                    $(o).parent().parent().remove();
                    loadArticleCates();
                }else {
                    if (json.Message!=undefined) {
                        layerAlert(json.Message);
                    }
                }
            }, "json");
        });
    }

    function linkToArticle(count,cid){
        if (count==0) { return;  }

    }

    function loadArticleByCid(){
        var cid=$(".selcate").val();
        loadArticleList(cid);
    }

    function delArticle(o,id) {
        layerConfirm("确定删除这篇文章吗？",function(){
            $.get("@Url.Action("DeleteArticle/")" + id, function (s) {
                if (s.State == 1) {
                    layerMsg('删除成功！');
                    $(o).parent().parent().remove();
                } else {
                    layerAlert(s.Message);
                }
            }, "json");
        });
    }
</script>

